#include <cstring>

#include "KmyMath.h"

#include "KmyFont.h"

KmyFont::KmyFont()
{
    cur_char = 0;
    src_font_buff = NULL;
}

KmyFont::~KmyFont(){
    if (is_mapping_table){
        delete[] mapping_tab.data[0];
        delete[] mapping_tab.data[1];
        delete[] mapping_tab.data;
        delete[] mapping_tab.char_code;
        delete[] mapping_tab.offset;
    }else{
        for (int i = 0; i<fullfont.range_count; i++){
            delete[] fullfont.data[i];
        }
        delete[] fullfont.start_code;
        delete[] fullfont.range;
        delete[] fullfont.data;
    }
    if (src_font_buff!=NULL){
        delete[] src_font_buff;
    }
}

int KmyFont::getAsciiFontDataSize(){
    int size = 0;
//    if (bit_per_point==2){
//        size = ascii_width*ascii_height>>2;
//    }else if (bit_per_point==1){
        size = ascii_width*ascii_height>>3;
//    }
    if (is_with_width){
        size+=4;
    }
    return size;
}

int KmyFont::getMbcsFontDataSize(){
    int size = 0;
//    if (bit_per_point==2){
//        size = mbcs_width*mbcs_height>>2;
//    }else if (bit_per_point==1){
        size = mbcs_width*mbcs_height>>3;
//    }
    if (is_with_width){
        size+=4;
    }
    return size;
}

u8* KmyFont::getUcs2Addr(u16 uc){
    u8* src;
    if (uc<256){
        int offset = (uc-32)*getAsciiFontDataSize();
        if (is_mapping_table){
            src = mapping_tab.data[0]+offset;
        }else{
            src = fullfont.data[0]+offset;
        }
    }else{
        if (is_mapping_table){
            // 映射表算法
            int u = mapping_tab.table_size-1;
            int d = 0;
            if (uc == mapping_tab.char_code[u]){
                src = mapping_tab.data[1]+mapping_tab.offset[u];
            }else if (uc == mapping_tab.char_code[d]){
                src = mapping_tab.data[1]+mapping_tab.offset[d];
            }else{
                int index = u/2;
                while (mapping_tab.char_code[index]!=uc){
                    if (uc>mapping_tab.char_code[index]){
                        d = index;
                        index = (u+index)/2;
                    }else{
                        u = index;
                        index = (d+index)/2;
                    }
                }
                src = mapping_tab.data[1]+mapping_tab.offset[index];
            }
        }else{
            // 完整字库算法
            int range_index = fullfont.range_count-1;
            while (uc<fullfont.start_code[range_index]){
                range_index--;
            }
            int offset = uc-fullfont.start_code[range_index];
            offset = offset*getMbcsFontDataSize();
            src = fullfont.data[range_index]+offset;
        }
    }
    return src;
}

int KmyFont::getUcs2Width(u16 uc){
    if (is_with_width){
        int* src = (int*)getUcs2Addr(uc);
        return *src;
    }else{
        if (uc<128){
            return ascii_width;
        }else{
            return mbcs_width;
        }
    }
}

u8* KmyFont::getFontBuff(u16 uc){
    if (src_font_buff==NULL){
        src_font_buff = new u8[mbcs_width*mbcs_height];
    }
    if (uc==cur_char){
        return src_font_buff;
    }else{
        u8 *p = getUcs2Addr(uc);
        int fh = mbcs_height;
        int fw = mbcs_width;
        if (uc<128){
            fw = ascii_width;
            fh = ascii_height;
        }
        if (is_with_width){
//            fw = *p;
            p+=4;
        }
        fw = ALIGN8(fw);
//        memset(src_font_buff, 0, fh*fw);
        for (int i=0; i<fh; i++){
            for (int j=0; j<fw; j+=8){
                src_font_buff[i*fw+j]  =*p&0x01;
                src_font_buff[i*fw+j+1]=*p&0x02;
                src_font_buff[i*fw+j+2]=*p&0x04;
                src_font_buff[i*fw+j+3]=*p&0x08;
                src_font_buff[i*fw+j+4]=*p&0x10;
                src_font_buff[i*fw+j+5]=*p&0x20;
                src_font_buff[i*fw+j+6]=*p&0x40;
                src_font_buff[i*fw+j+7]=*p&0x80;
                p++;
            }
        }
    }
    return src_font_buff;
}
